﻿namespace com.stifel.BranchMgmtDao
{
    using System;
    using System.Collections.Generic;
    using System.Linq;

    using com.stifel.Extensions.SharePointExtensions;

    using Microsoft.SharePoint;
    using Microsoft.SharePoint.Linq;

    using Context = BranchMgmtDataContext;

    using CustomObject = StifelNotification;

    public class StifelNotificationDao : StifelBaseDao
    {
        #region Fields

        public const string ListName = "stifelbranchnotification";

        #endregion Fields

        #region Constructors

        public StifelNotificationDao( )
            : base(ListName)
        {
            CurrentWebUrl = ConfiguredWebUrl;
        }

        public StifelNotificationDao( string currentWebUrl )
            : base(ListName)
        {
            this.CurrentWebUrl = currentWebUrl;
        }

        #endregion Constructors

        #region Methods

        public static bool ListExists( string siteUrl )
        {
            bool ok = false;
            try
            {
                using ( SPSite spSite = new SPSite( siteUrl ) )
                {
                    string s = siteUrl.Replace( spSite.Url, string.Empty );
                    s = s.StartsWith( "/" ) ? s.Substring( 1 ) : s;
                    using ( SPWeb spWeb = spSite.OpenWeb( s ) )
                    {
                        ok = spWeb.Lists.ContainsList( ListName );
                    }
                }
            }
            catch
            {
            }
            return ok;
        }

        public Guid Add( CustomObject value )
        {
            using ( Context c = new Context( CurrentWebUrl ) )
            {
                EntityList<CustomObject> l = c.GetList<CustomObject>( ListTitle );
                string title = value.Title;
                Guid g = Guid.NewGuid( );
                value.Title = g.ToString( );
                l.InsertOnSubmit( clean( value ) );
                c.SubmitChanges( );
                CustomObject v = ( from o in l
                                   where o.Title == g.ToString( )
                                   select o ).SingleOrDefault( );
                v.Title = title;
                c.SubmitChanges( );

                return v.UniqueId;
            }
        }

        public void Clear( )
        {
            using ( Context c = new Context( CurrentWebUrl ) )
            {
                EntityList<CustomObject> l = c.GetList<CustomObject>( ListTitle );
                var v = ( from o in l
                          where o.Id > 0
                          select o );
                if ( v.Count( ) > 0 )
                {
                    l.DeleteAllOnSubmit( v );
                    c.SubmitChanges( );
                }
            }
        }

        public void Delete( Guid id )
        {
            using ( Context c = new Context( CurrentWebUrl ) )
            {
                EntityList<CustomObject> l = c.GetList<CustomObject>( ListTitle );
                CustomObject v = ( from o in l
                                   where o.UniqueId == id
                                   select o ).SingleOrDefault( );
                if ( v != null )
                    l.DeleteOnSubmit( v );
            }
        }

        public CustomObject Get( int id )
        {
            using ( Context c = new Context( CurrentWebUrl ) )
            {
                EntityList<CustomObject> l = c.GetList<CustomObject>( ListTitle );
                CustomObject v = ( from o in l
                                   where o.Id.HasValue && o.Id.Value == id
                                   select o ).SingleOrDefault( );
                return v;
            }
        }

        public CustomObject Get( Guid id )
        {
            using ( Context c = new Context( CurrentWebUrl ) )
            {
                EntityList<CustomObject> l = c.GetList<CustomObject>( ListTitle );
                CustomObject v = ( from o in l
                                   where o.UniqueId == id
                                   select o ).SingleOrDefault( );
                return v;
            }
        }

        public List<CustomObject> GetAll( )
        {
            using ( Context c = new Context( CurrentWebUrl ) )
            {
                List<CustomObject> l = c.GetList<CustomObject>( ListTitle ).ToList( );
                return l;
            }
        }

        CustomObject clean( CustomObject value )
        {
            return new CustomObject
            {
                BranchLookupId = value.BranchLookupId,
                BranchNotification = value.BranchNotification,
                BranchNotificationURL = value.BranchNotificationURL,
                Title = value.Title
            };
        }

        #endregion Methods
    }
}